package team2.arsystem.GUI;

import team2.arsystem.Controller.ApplicationController;
import team2.arsystem.Utilities.CommandConstants;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EditMemberScreen extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.addnewmemberscreen);
		
		TextView label = (TextView)findViewById(R.id.tvMemberLabel);
		label.setText("Edit Member");
		
		final EditText firstNameEditText = (EditText)findViewById(R.id.editTextFirstName);
		final EditText middleNameEditText = (EditText)findViewById(R.id.editTextMiddleName);
		final EditText lastNameEditText = (EditText)findViewById(R.id.editTextLastName);
		final EditText mobileNumberEditText = (EditText)findViewById(R.id.editTextMobileNumber);
		Button okButton = (Button)findViewById(R.id.buttonOKAddMember);
	    Button cancelButton = (Button)findViewById(R.id.buttonCancelAddMember);
	    
	    
		final String oldName = getIntent().getExtras().getString("team2.arsystem.GUI.ViewMembers.memberName");
		final String oldMobileNumber = getIntent().getExtras().getString("team2.arsystem.GUI.ViewMembers.memberMobileNumber");
		
		String oldNameCopy = oldName;
		
		firstNameEditText.setText(oldNameCopy.substring(0,oldName.indexOf(' ')));
		oldNameCopy = oldNameCopy.substring(oldNameCopy.indexOf(' ')+1);
		Log.i("1 chop",oldNameCopy);
		middleNameEditText.setText(oldNameCopy.substring(0,oldNameCopy.indexOf(' ')));
		oldNameCopy = oldNameCopy.substring(oldNameCopy.indexOf(' ')+1);
		Log.i("2 chop",oldNameCopy);
		lastNameEditText.setText(oldNameCopy);
		
		mobileNumberEditText.setText(oldMobileNumber);
		    
	    /* If OK button is clicked*/
	    okButton.setOnClickListener(new OnClickListener() {
				
	        	public void onClick(View v) {
	        		
	        	String[] memberDetails = new String[6];	
	        	memberDetails[0] = firstNameEditText.getText().toString();
	        	memberDetails[1] = middleNameEditText.getText().toString();
	        	memberDetails[2] = lastNameEditText.getText().toString();
	        	memberDetails[3] = mobileNumberEditText.getText().toString();
	        	memberDetails[4] = oldName;
	        	memberDetails[5] = oldMobileNumber;
	        	
	        	if((ApplicationController.getApplicationController().passCommand(CommandConstants.editMember,memberDetails)))
	        	{
	        		/*Member was added successfully*/
	        		try
	        		{
	        			Dialog dialogOK = new Dialog(EditMemberScreen.this);
	        			dialogOK.setContentView(R.layout.popupbox);
	        			dialogOK.setTitle("Account Reminder System");
	        			
	        		        			
	        			TextView dialogText = (TextView)dialogOK.findViewById(R.id.textViewDialogMessage);
	        			dialogText.setText("The member details was successfully edited");
	        			
	        			
	                    Button button = (Button) dialogOK.findViewById(R.id.buttonDialogOK);
	                   
	                    button.setOnClickListener(new OnClickListener() {
	                   
	                        public void onClick(View v) {
	                        	
	                        	try
	                        	{
	                        	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"ViewMembers");
	                    		 Intent myIntent = new Intent(EditMemberScreen.this,menuScreen);
	                    		 startActivity(myIntent);
	                        	}
	                        	catch(Exception e){
	                    			 e.printStackTrace();
	                    		 }
	                    		 
	                        }
	                    });
	        		    
	            		 
	                    dialogOK.show();
	        		}
	        		catch(Exception e)
	        		{
	        			 e.printStackTrace();
	        		}
	        	}
	        		
				}
			});
	        
	         /* If Cancel button is clicked*/
	        cancelButton.setOnClickListener(new OnClickListener() {
				
	        	public void onClick(View v) {
	        		
	        	  	try
	                {
	                   	 Class menuScreen = Class.forName("team2.arsystem.GUI."+"ViewMembers");
	                	 Intent myIntent = new Intent(EditMemberScreen.this,menuScreen);
	                	 startActivity(myIntent);
	                }
	                catch(Exception e)
	               	{
	               		 e.printStackTrace();
	                }
	                    		 
	            }
	        	
	        });
	        		  
	}

}
